//
//  ActivityViewController.m
//  项目三
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "ActivityViewController.h"
static NSString *const kCellWithReuseIdentifier = @"simpleCell";

@interface ActivityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>


@property (weak, nonatomic) IBOutlet UICollectionView *activityCollectionView;

@property (nonatomic, strong) NSArray *dataArray /**<  */;

- (void) initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
    UINib *cellNib = [UINib nibWithNibName:@"ZLCollectionViewCell" bundle:nil];
    [self.activityCollectionView registerNib:cellNib forCellWithReuseIdentifier:kCellWithReuseIdentifier];

    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setItemSize:DHFlexibleSize(CGSizeMake(310, 210))];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 20, 20);
    [self.activityCollectionView setCollectionViewLayout:flowLayout animated:YES];
}



- (CAAnimationGroup *)rotateAnimationFromAngle :(CGFloat)angle {
    
    CGFloat duration = 1;
    
    CABasicAnimation *animat = [CABasicAnimation animation];
    animat.keyPath = @"transform.rotation.z";
    animat.duration = duration;
    animat.fromValue = @(angle);
    
    CABasicAnimation *positionAnimat = [CABasicAnimation animation];
    positionAnimat.keyPath = @"position";
    positionAnimat.fromValue = [NSValue valueWithCGPoint:DHFlexibleCenter(CGPointMake(380, 480))];
    animat.duration = duration;
    
    CABasicAnimation *alpAnimat = [CABasicAnimation animation];
    alpAnimat.keyPath = @"opacity";
    alpAnimat.fromValue = @0.0;
    alpAnimat.duration = duration;
    
    CAAnimationGroup *animateGroup = [CAAnimationGroup animation];
    animateGroup.animations = @[animat, positionAnimat, alpAnimat];
    animateGroup.duration = duration;
    return animateGroup;
}

- (void)initializeDataSource {
    NSMutableArray *firstSection = [[NSMutableArray alloc] init];
    NSMutableArray *secondSection = [[NSMutableArray alloc] init];
    for (int i=0; i<3; i++){
        [firstSection addObject:[NSString stringWithFormat:@"单元格 %d", i]];
        [secondSection addObject:[NSString stringWithFormat:@"数据项 %d", i]];
    }
    self.dataArray = [[NSArray alloc] initWithObjects:firstSection, secondSection, nil];
}

- (void)initializeUserInterface {
    
    self.titleImageView.image = [UIImage imageNamed:@"活动交流_05"];
     NSLog(@"%lf",self.view.frame.size.height);
    [super initializeUserInterface];
}

#pragma mark <UICollectionViewDataSource, UICollectionViewDelegate>
// 多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
// 每组多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    NSMutableArray *sectionArray = [_dataArray objectAtIndex:section];
    return 5;
}
// 每个cell显示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellWithReuseIdentifier forIndexPath:indexPath];
//    cell.center = DHFlexibleCenter(CGPointMake(356, 526));
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView.contentOffset.y == 0) {
        [cell.layer addAnimation:[self rotateAnimationFromAngle:M_PI_4 * indexPath.row] forKey:@"sdf"];
    } else {
        [cell.layer removeAnimationForKey:@"sdf"];
    }
}

@end
